﻿using Assets.Scripts.Manager;
using DG.Tweening;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class StartView : MonoBehaviour
{
    #region Unity
    private void OnEnable()
    {
        AddInputEvent();
    }

    private void OnDisable()
    {
        RemoveInputEvent();
    }
    private void Start()
    {
        Show(_selectIndex);
    }


    #endregion
    [SerializeField] private Image _startIcon;
    [SerializeField] private Image _start;

    [SerializeField] private Image _continueIcon;
    [SerializeField] private Image _continue;

    private int _selectIndex=0;

    public void Show(int i=0) 
    {
        switch (i)
        {
            case 0:
                _startIcon.gameObject.SetActive(true);
                _continueIcon.gameObject.SetActive(false);
                break;
            case 1:
                _startIcon.gameObject.SetActive(false);
                _continueIcon.gameObject.SetActive(true);
                break;
            default:
                break;
        }
        _selectIndex = i;
    }

    public void Click() 
    {
        switch (_selectIndex)
        {
            case 0:
                StartGame();
                break;
            case 1:
                ContinueGame();
                break;
        }
    }

    #region 开始游戏
    [SerializeField] private float intervalTime;
    private void StartGame()
    {
        _start.DOFade(0, intervalTime).SetEase(Ease.Flash, 4, 0.5f).OnComplete(() => {
            GameSystem.Instance.ChangeGameState(State.Load);
        });
    }
    #endregion

    #region 继续游戏
    private void ContinueGame()
    {
        _continue.DOFade(0, intervalTime).SetEase(Ease.Flash, 4, 0.5f);
    }
    #endregion


    #region Input 
    private void AddInputEvent()
    {
        InputManager.Instance.OnSelectClickEvent += OnSelectClicked;
        InputManager.Instance.OnStartClickEvent += OnStartClicked;
    }
    private void RemoveInputEvent()
    {
        InputManager.Instance.OnSelectClickEvent -= OnSelectClicked;
        InputManager.Instance.OnStartClickEvent -= OnStartClicked;
    }
    
    private void OnSelectClicked()
    {
        AudioManager.Instance.PlaySound(SoundType.Select);
        if (_selectIndex == 0) 
        {
            _selectIndex = 1;
        }else if (_selectIndex == 1) 
        {
            _selectIndex = 0;
        }
        Show(_selectIndex);
    }
    private void OnStartClicked()
    {
        AudioManager.Instance.PlaySound(SoundType.Start);
        Click();
    }

    


    #endregion
}
